<?php

class Common_AjaxForm extends Zend_Form
{
    public function render(Zend_View_Interface $view = null)
    {
        $html = parent::render($view);
        $html .= '<script type="text/javascript">
            myform = $("#' . $this->getName() . '");            
            files = myform.find(\'input[type="file"]\');
            myform.attr("uploadedFiles",0)
            myform.ajaxForm({
                success: function(){
                    $(".upload_file_info").remove();
                }
            });
            submits = myform.find(\'input[type="submit"]\');
            if(files.length > 0){
                $.each(files, function(i, val){
                    $(val).uploadify({
                        "uploader"  : "/swf/uploadify.swf",
                        "script"    : "/files/index/upload",
                        "cancelImg" : "/images/cancel.png",
                        "folder"    : "/uploads",
                        "auto"      : false,
                        "multi"     : true,
                        "scriptData": {"fieldname": $(val).attr("name")},
                        "onComplete"  : function(event, ID, fileObj, response, data) {
                          $(val).parent().append(response)
                          myform.attr("uploadedFiles",parseInt(myform.attr("uploadedFiles")) + 1);
                        },
                        "onAllComplete": function(){                            
                            if(myform.attr("uploadedFiles") == $(".uploadifyQueueItem").length){
                                myform.submit();
                            }
                        }
                    })
                })    
                submits.click(function(){
                    if($(".uploadifyQueueItem").length > 0){
                        files.uploadifyUpload();
                        return false;
                    }    
                })
            }
</script>';
        return $html;
    }
}